﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Timers;

namespace InscriberDataManager
{
    /// <summary>
    /// Interaction logic for Window1.xaml
    /// </summary>
    public partial class Window1 : Window
    {
        Timer timer = new Timer();
        int indexlocation = -1;
        Functionality.Reports rpselected = null;

        ListBox dragSource = null;

        public Window1()
        {
            InitializeComponent();
           // this.InitializeDynamic();
            Functionality.readXMLParameters();
            Functionality.getreports(listBox3);
            Functionality.getRuningLines(listBox2,"VISOS");
            Functionality.getRuningLinesTypes(comboBox1);
            Functionality.getLogoTemplates(comboBox2);
            Functionality.getAnonsaiPartsByANId("1", listBox10);
           // textBox2.MaxLength = int.Parse(Functionality.MaxRLLength);
            comboBox4.Text = "VISOS";
            comboBox3.Text = "Pradžia";
            checkBox3.IsChecked = true;
            timer.Elapsed += new ElapsedEventHandler(timer_Tick);
            timer.Interval = (1000) * (5);             
            timer.Enabled = true;                       
            timer.Start();     
            
            //System.Globalization.CultureInfo ci = System.Threading.Thread.CurrentThread.CurrentCulture;
            //InputLanguageManager.SetInputLanguage(textBox2, ci);
        }

        void timer_Tick(object sender, ElapsedEventArgs e)
        {
            App.Current.Dispatcher.Invoke((Action)delegate()
            {
                Functionality.getreports(listBox3);
            });
        }


        private void MenuItem_Click(object sender, RoutedEventArgs e)
        {

        }

        private void MenuItem_Click_1(object sender, RoutedEventArgs e)
        {
            this.Close();
        }

        private void button1_Click(object sender, RoutedEventArgs e)
        {
            if (!textBox1.Text.Equals(""))
            {
                listBox1.Items.Add(new Functionality.Reports() { id = 0, name=textBox1.Text, status="0"});
            }
            textBox1.Text = "";
            button2_Click(this, null);
        }

        private void button3_Click(object sender, RoutedEventArgs e)
        {
            int location = listBox1.SelectedIndex;
            if (location > 0)
            {
                object rememberMe = listBox1.SelectedItem;
                listBox1.Items.RemoveAt(location);
                listBox1.Items.Insert(location - 1, rememberMe);
                listBox1.SelectedIndex = location - 1;
            }

        }

        private void button5_Click(object sender, RoutedEventArgs e)
        {
            int location = listBox1.SelectedIndex;
            if (location < listBox1.Items.Count-1)
            {
                object rememberMe = listBox1.SelectedItem;
                listBox1.Items.RemoveAt(location);
                listBox1.Items.Insert(location + 1, rememberMe);
                listBox1.SelectedIndex = location + 1;
            }
        }

        private void button4_Click(object sender, RoutedEventArgs e)
        {
            System.Collections.IList ls = listBox1.SelectedItems;
            for (int i = 0; i < ls.Count; i++)
            {
                int location = listBox1.Items.IndexOf(ls[i]);
                Functionality.Reports rp = (Functionality.Reports)listBox1.Items[location];
                if (rp.id != 0)
                {
                    Functionality.removeReportById(rp.id);
                    listBox1.Items.RemoveAt(location);

                }
                else
                {
                    listBox1.Items.RemoveAt(location);
                }
            }
            button2_Click(this, null);
        }

        private void button2_Click(object sender, RoutedEventArgs e)
        {
            Functionality.saveReports(listBox1);
            Functionality.getreports(listBox1);
        }

        private void listBox2_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            int location = listBox2.SelectedIndex;
            if (location != -1)
            {
                Functionality.RuningLines rl = (Functionality.RuningLines)listBox2.Items[location];
                textBox2.Text = rl.Line;
                comboBox1.Text = rl.Type;
                comboBox3.Text = rl.End;
                Functionality.CurrentRLid = rl.id;
                Functionality.CurrentRLindex = location;
            }
        }

        private void Image_ImageFailed(object sender, ExceptionRoutedEventArgs e)
        {

        }

        private void button7_Click(object sender, RoutedEventArgs e)
        {
            if (comboBox4.Text.Equals("VISOS"))
            {
                if (MessageBox.Show("Ar tikrai norite trinti visas eilutes?", "Užklausa", MessageBoxButton.YesNo, MessageBoxImage.Asterisk, MessageBoxResult.No) == MessageBoxResult.Yes)
                {
                    Functionality.removeAllRL();
                    Functionality.getRuningLines(listBox2,"VISOS");
                }
            }
            else
            {

                int location = listBox2.SelectedIndex;
                Functionality.RuningLines rp = (Functionality.RuningLines)listBox2.Items[location];
                if (rp.id != 0)
                {
                    //Functionality.removeRuningLineById(rp.id);
                    listBox2.Items.RemoveAt(location);

                }
                else
                {
                    listBox2.Items.RemoveAt(location);
                }
            }
          /*  System.Collections.IList ls = listBox2.SelectedItems;
            for (int i = 0; i < ls.Count; i++)
            {
                int location = listBox2.Items.IndexOf(ls[i]);
                Functionality.RuningLines rp = (Functionality.RuningLines)listBox2.Items[location];
                if (rp.id != 0)
                {
                    //Functionality.removeReportById(rp.id);
                    listBox2.Items.RemoveAt(location);

                }
                else
                {
                    listBox2.Items.RemoveAt(location);
                }
            }*/
        }

        private void button6_Click(object sender, RoutedEventArgs e)
        {
            Functionality.saveRuningLine(listBox2,textBox2,comboBox1,comboBox3,comboBox4.Text);
            Functionality.getRuningLines(listBox2,comboBox4.Text);
            Functionality.CurrentRLid = 0;
        }

        private void button8_Click(object sender, RoutedEventArgs e)
        {
            Functionality.getreports(listBox3);
        }

        private void tabItem2_MouseDown(object sender, MouseButtonEventArgs e)
        {
        }

        private void tabControl1_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
          /*  if (tabControl1.SelectedIndex == 1)
            {
                Functionality.getreports(listBox3);
            }*/
        }

        private void listBox3_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
           
        }

        private void listBox4_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            int location = listBox4.SelectedIndex;
            if (location >= 0)
            {
                Functionality.Anonsai an = (Functionality.Anonsai)listBox4.Items[location];
                Functionality.getAnonsaiPartsByANId(an.id.ToString(), listBox5);
                listBox5.IsEnabled = true;
                textBox4.IsEnabled = true;
                button10.IsEnabled = true;
                button12.IsEnabled = true;
            }
        }

        private void button9_Click(object sender, RoutedEventArgs e)
        {
            
        }

        private void button9_Click_1(object sender, RoutedEventArgs e)
        {
            int location = Functionality.CurrentRPindex;
            if (location >= 0 && !textBox3.Text.Equals(""))
            {
                //Functionality.Reports rp = (Functionality.Reports)listBox3.Items[location];

                Functionality.AddAnonsas(Functionality.CurrentRPid.ToString(), textBox3.Text);
                Functionality.getAnonsaiByRepId(Functionality.CurrentRPid.ToString(), listBox4);
                listBox5.Items.Clear();
                listBox5.IsEnabled = false;
                textBox4.IsEnabled = false;
                button10.IsEnabled = false;
                button12.IsEnabled = false;
                textBox3.Text = "";
            }
        }

        private void button10_Click(object sender, RoutedEventArgs e)
        {
            int location = listBox4.SelectedIndex;
            if (location >= 0 && !textBox4.Text.Equals(""))
            {
                Functionality.Anonsai an = (Functionality.Anonsai)listBox4.Items[location];

                Functionality.AddAnonsasPart(an.id.ToString(), Functionality.rpl(textBox4.Text.ToUpper()));
                Functionality.getAnonsaiPartsByANId(an.id.ToString(), listBox5);
                textBox4.Text = "";
            }
        }

        private void button11_Click(object sender, RoutedEventArgs e)
        {
            int location = listBox4.SelectedIndex;
            if (location >= 0)
            {
                Functionality.Anonsai an = (Functionality.Anonsai)listBox4.Items[location];
                Functionality.removeAnonsaiById(an.id);
                listBox4.Items.RemoveAt(location);
                listBox5.Items.Clear();
                listBox5.IsEnabled = false;
                textBox4.IsEnabled = false;
                button10.IsEnabled = false;
                button12.IsEnabled = false;
            }

        }

        private void button12_Click(object sender, RoutedEventArgs e)
        {
            int location = listBox5.SelectedIndex;
            if (location >= 0)
            {
                Functionality.AnonsaiParts an = (Functionality.AnonsaiParts)listBox5.Items[location];
                Functionality.AnonsaiPartsById(an.id);
                listBox5.Items.RemoveAt(location);

            }
        }

        private void listBox6_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            int location = listBox6.SelectedIndex;
            if (location >= 0)
            {
                Functionality.Lydai ld = (Functionality.Lydai)listBox6.Items[location];
                Functionality.getLydaiPartsByANId(ld.id.ToString(), listBox7);
                listBox7.IsEnabled = true;
                textBox6.IsEnabled = true;
                textBox7.IsEnabled = true;
                button14.IsEnabled = true;
                button15.IsEnabled = true;
                textBox5.Text = ld.title;
                bool ch=false;
                if (ld.template.Equals("N")){
                  ch=false;
                }else{
                    ch=true;
                }
                checkBox1.IsChecked = ch;
                Functionality.CurrentLDid = ld.id;
                Functionality.CurrentLDindex = location;
            }

        }

        private void button13_Click(object sender, RoutedEventArgs e)
        {
            int location = Functionality.CurrentRPindex;
            if (location >= 0 && !textBox5.Text.Equals(""))
            {
               // Functionality.Reports rp = (Functionality.Reports)listBox3.Items[location];

                Functionality.AddLydai(Functionality.CurrentRPid.ToString(), textBox5, checkBox1);
                Functionality.getLydaiByRepId(Functionality.CurrentRPid.ToString(), listBox6);
                listBox7.Items.Clear();
                listBox7.IsEnabled = false;
                textBox6.IsEnabled = false;
                textBox7.IsEnabled = false;
                button14.IsEnabled = false;
                button15.IsEnabled = false;
                textBox5.Text = "";
                checkBox1.IsChecked = false;
            }
        }

        private void button16_Click(object sender, RoutedEventArgs e)
        {
            int location = listBox6.SelectedIndex;
            if (location >= 0)
            {
                Functionality.Lydai ld = (Functionality.Lydai)listBox6.Items[location];
                Functionality.removeLydaiById(ld.id);
                listBox6.Items.RemoveAt(location);
                listBox7.Items.Clear();
                listBox7.IsEnabled = false;
                textBox6.IsEnabled = false;
                textBox7.IsEnabled = false;
                button14.IsEnabled = false;
                button15.IsEnabled = false;
            }
        }

        private void button14_Click(object sender, RoutedEventArgs e)
        {
            int location = listBox6.SelectedIndex;
            if (location >= 0 && !textBox6.Text.Equals("") && !textBox7.Text.Equals(""))
            {
                Functionality.Lydai ld = (Functionality.Lydai)listBox6.Items[location];

                Functionality.AddLydaiPart(ld.id.ToString(), Functionality.rpl(textBox7.Text.ToUpper()), Functionality.rpl(textBox6.Text.ToUpper()));
                Functionality.getLydaiPartsByANId(ld.id.ToString(), listBox7);
                textBox6.Text = "";
                textBox7.Text = "";
                Functionality.updateLydaiCount(ld.id.ToString(), listBox7);
            }
        }

        private void button15_Click(object sender, RoutedEventArgs e)
        {
            int location = listBox7.SelectedIndex;
            if (location >= 0)
            {
                Functionality.LydaiParts ld = (Functionality.LydaiParts)listBox7.Items[location];
                Functionality.removeLydaiPartsById(ld.id);
                listBox7.Items.RemoveAt(location);
                Functionality.updateLydaiCount(ld.lead_id.ToString(), listBox7);
            }

        }

       

        private void button17_Click(object sender, RoutedEventArgs e)
        {
            int location = Functionality.CurrentRPindex;
            if (location >= 0  && !comboBox2.Text.Equals(""))
            {
                if (Functionality.checkTime(textBox10.Text, textBox11.Text))
                {
                //Functionality.Reports rp = (Functionality.Reports)listBox3.Items[location];

                int loc = comboBox2.SelectedIndex;
                Functionality.logosTemplates lt = (Functionality.logosTemplates)comboBox2.Items[loc];
                string template = lt.Template;
                checkBox2.IsChecked = true;
                Functionality.AddLogo(Functionality.CurrentRPid.ToString(), lt.TemplateName, textBox9, template, textBox10, textBox11, checkBox2);
                Functionality.getLogoByRepId(Functionality.CurrentRPid.ToString(), listBox8);
                textBox9.Text = "";
                textBox10.Text = "00:00";
                textBox11.Text = "00:00";
                checkBox2.IsChecked = false;
                }else{MessageBox.Show("Start laikas turi būti mažesnis už Stop laiką!");}
            }
        }

        private void button20_Click(object sender, RoutedEventArgs e)
        {
            int location = Functionality.CurrentRPindex;
            if (location >= 0 && !textBox12.Text.Equals("") && !textBox13.Text.Equals(""))
            {
                if (Functionality.checkTime(textBox14.Text,textBox15.Text))
                {
                    //Functionality.Reports rp = (Functionality.Reports)listBox3.Items[location];

                    Functionality.AddTitras(Functionality.CurrentRPid.ToString(), textBox12, textBox13, textBox14, textBox15);
                    Functionality.getTitraiByRepId(Functionality.CurrentRPid.ToString(), listBox9);
                    textBox12.Text = "";
                    textBox13.Text = "";
                    textBox14.Text = "00:00";
                    textBox15.Text = "00:00";
                }else{MessageBox.Show("Start laikas turi būti mažesnis už Stop laiką!");}
            }
        }

        private void button19_Click(object sender, RoutedEventArgs e)
        {
            int location = listBox8.SelectedIndex;
            if (location >= 0)
            {
                Functionality.logo lg = (Functionality.logo)listBox8.Items[location];
                Functionality.removeLogoById(lg.id);
                listBox8.Items.RemoveAt(location);
               
            }
        }

        private void button22_Click(object sender, RoutedEventArgs e)
        {
            int location = listBox9.SelectedIndex;
            if (location >= 0)
            {
                Functionality.Titras tr = (Functionality.Titras)listBox9.Items[location];
                Functionality.removeTitrasById(tr.id);
                listBox9.Items.RemoveAt(location);

            }
        }

        private void listBox8_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            int location = listBox8.SelectedIndex;
            if (location != -1)
            {
                Functionality.logo lg = (Functionality.logo)listBox8.Items[location];
                textBox9.Text = lg.tekstas;
                textBox10.Text = lg.start_time;
                textBox11.Text = lg.end_time;
                if (lg.reverse.Equals("True"))
                {
                    checkBox2.IsChecked = true;
                }
                else
                {
                    checkBox2.IsChecked = false;
                }
                string tmp = "";
                int ix = -1;
                Functionality.logosTemplates item=null;
                for (int i=0;i<comboBox2.Items.Count;i++){
                    Functionality.logosTemplates lgt = (Functionality.logosTemplates)comboBox2.Items[i];
                    if (lgt.Template.Equals(lg.template)){
                        tmp = lgt.TemplateName;
                        ix = i;
                        item=lgt;
                    }
                }
               // comboBox2.SetValue(= ix;
               // comboBox2.Text = tmp;
                comboBox2.SelectedItem = item;
                Functionality.CurrentLGid = lg.id;
                Functionality.CurrentLGindex = location;
            }
        }

        private void listBox9_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            int location = listBox9.SelectedIndex;
            if (location != -1)
            {
                Functionality.Titras tr = (Functionality.Titras)listBox9.Items[location];
                textBox12.Text = tr.asmuo;
                textBox13.Text = tr.pareigos;
                textBox14.Text = tr.start_time;
                textBox15.Text = tr.end_time;
                Functionality.CurrentTRid = tr.id;
                Functionality.CurrentTRindex = location;
            }
        }

        private void button21_Click(object sender, RoutedEventArgs e)
        {
            int location = Functionality.CurrentRPindex;
            if (location >= 0 && !textBox12.Text.Equals("") && !textBox13.Text.Equals(""))
            {
                if (Functionality.checkTime(textBox14.Text,textBox15.Text))
                {
               // Functionality.Reports rp = (Functionality.Reports)listBox3.Items[location];

                Functionality.EditTitras(textBox12, textBox13, textBox14, textBox15);
                Functionality.getTitraiByRepId(Functionality.CurrentRPid.ToString(), listBox9);
                textBox12.Text = "";
                textBox13.Text = "";
                textBox14.Text = "00:00";
                textBox15.Text = "00:00";
                }
                else { MessageBox.Show("Start laikas turi būti mažesnis už Stop laiką!"); }
            }
        }

        private void button18_Click(object sender, RoutedEventArgs e)
        {
            int location = Functionality.CurrentRPindex;
            if (location >= 0  && comboBox2.SelectedIndex!=-1)
            {
                if (Functionality.checkTime(textBox10.Text, textBox11.Text))
                {
                   // Functionality.Reports rp = (Functionality.Reports)listBox3.Items[location];
                    int loc = comboBox2.SelectedIndex;
                    Functionality.logosTemplates lt = (Functionality.logosTemplates)comboBox2.Items[loc];
                    string template = lt.Template;
                    checkBox2.IsChecked = true;
                    Functionality.EditLogo(lt.TemplateName, textBox9, template, textBox10, textBox11, checkBox2);
                    Functionality.getLogoByRepId(Functionality.CurrentRPid.ToString(), listBox8);
                    textBox9.Text = "";
                    textBox10.Text = "00:00";
                    textBox11.Text = "00:00";
                    comboBox2.Text = "";
                    checkBox2.IsChecked = false;
                }
            }
        }

        private void listBox2_LostFocus(object sender, RoutedEventArgs e)
        {
            
        }

        private void listBox2_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Delete)
            {
                int location = listBox2.SelectedIndex;
                Functionality.RuningLines rp = (Functionality.RuningLines)listBox2.Items[location];
                if (rp.id != 0)
                {
                    //Functionality.removeRuningLineById(rp.id);
                    listBox2.Items.RemoveAt(location);

                }
                else
                {
                    listBox2.Items.RemoveAt(location);
                }
            }
        }

        private void button23_Click(object sender, RoutedEventArgs e)
        {
            comboBox3.Text = "Pradžia";
            comboBox1.SelectedIndex = 0;
            textBox2.Text = "";
            Functionality.CurrentRLid = 0;
            Functionality.CurrentRLindex = 0;
            comboBox1.Text = comboBox4.Text;
        }

        private void textBox7_PreviewKeyUp(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Enter)
            {
                textBox6.Focus();
            }
            
        }

        private void button24_Click(object sender, RoutedEventArgs e)
        {
            Functionality.insertRuningLine(listBox2, textBox2, comboBox1, comboBox3);
            Functionality.CurrentRLid = 0;
        }

        private void button26_Click(object sender, RoutedEventArgs e)
        {
            int location = listBox2.SelectedIndex;
            if (location > 0)
            {
                object rememberMe = listBox2.SelectedItem;
                listBox2.Items.RemoveAt(location);
                listBox2.Items.Insert(location - 1, rememberMe);
                listBox2.SelectedIndex = location - 1;
            }
        }

        private void button25_Click(object sender, RoutedEventArgs e)
        {
            int location = listBox2.SelectedIndex;
            if (location < listBox2.Items.Count - 1)
            {
                object rememberMe = listBox2.SelectedItem;
                listBox2.Items.RemoveAt(location);
                listBox2.Items.Insert(location + 1, rememberMe);
                listBox2.SelectedIndex = location + 1;
            }
        }

        private void comboBox4_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            ListBoxItem selectedItem = (ListBoxItem)comboBox4.SelectedItem;
            Functionality.getRuningLines(listBox2, selectedItem.Content.ToString());
            comboBox1.Text = selectedItem.Content.ToString();
        }

        private void Image_ImageFailed_1(object sender, ExceptionRoutedEventArgs e)
        {

        }

        private void textBox2_TextChanged(object sender, TextChangedEventArgs e)
        {
            //MessageBox.Show(e.Source.ToString());
        }

        private void textBox2_PreviewTextInput(object sender, TextCompositionEventArgs e)
        {
            //MessageBox.Show(e.Source.ToString());
        }

        private void listBox1_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            int location = listBox1.SelectedIndex;
            if (location != -1)
            {
                Functionality.Reports rp = (Functionality.Reports)listBox1.Items[location];
                textBox1.Text = rp.name;
                Functionality.CurrentRPid = rp.id;
                Functionality.CurrentRPindex = location;
            }
        }

        private void button27_Click(object sender, RoutedEventArgs e)
        {
            int location = listBox1.SelectedIndex;
            if (location >= 0 && !textBox1.Text.Equals(""))
            {
                Functionality.Reports rp = (Functionality.Reports)listBox1.Items[location];
                
                listBox1.Items.Insert(location,new Functionality.Reports(){id=rp.id,name=textBox1.Text,status=rp.status});
                listBox1.Items.RemoveAt(location+1);   
            // Functionality.EditLogo(textBox8, textBox9, template, textBox10, textBox11, checkBox2);
                //Functionality.getreports(listBox1);
                textBox1.Text = "";
            }
            button2_Click(this, null);
        }

        private void listBox7_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            int location = listBox7.SelectedIndex;
            if (location >= 0)
            {
                Functionality.LydaiParts ldP = (Functionality.LydaiParts)listBox7.Items[location];
                textBox7.Text = ldP.tekstas1;
                textBox6.Text = ldP.tekstas2;
                Functionality.CurrentLDPartsid = ldP.id;
                Functionality.CurrentLDPartsindex = location;
            }
        }

        private void button28_Click(object sender, RoutedEventArgs e)
        {
            int location = Functionality.CurrentRPindex;
            if (location >= 0 && !textBox5.Text.Equals("") )
            {
               // Functionality.Reports rp = (Functionality.Reports)listBox3.Items[location];

                Functionality.EditLydai(textBox5, checkBox1);
                Functionality.getLydaiByRepId(Functionality.CurrentRPid.ToString(), listBox6);
                textBox5.Text = "";
                checkBox1.IsChecked = false;
            }
        }

        private void button29_Click(object sender, RoutedEventArgs e)
        {
            int location = listBox6.SelectedIndex;
            if (location >= 0 && !textBox7.Text.Equals("") && !textBox6.Text.Equals(""))
            {
                Functionality.Lydai ld = (Functionality.Lydai)listBox6.Items[location];

                Functionality.EditLydaiParts(textBox7, textBox6);
                Functionality.getLydaiPartsByANId(ld.id.ToString(), listBox7);
                textBox7.Text = "";
                textBox6.Text = "";
             }
        }

        private void button33_Click(object sender, RoutedEventArgs e)
        {
            Functionality.getAnonsaiPartsByANId("1", listBox10);
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {

        }

        private void listBox10_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            int location = listBox10.SelectedIndex;
            if (location >= 0)
            {
                Functionality.AnonsaiParts an = (Functionality.AnonsaiParts)listBox10.Items[location];
                textBox8.Text = an.tekstas;
                Functionality.CurrentANPartsid = an.id;
                Functionality.CurrentANPartsindex = location;
            }
        }

        private void button31_Click(object sender, RoutedEventArgs e)
        {
            int location = listBox10.SelectedIndex;
            if (location >= 0)
            {
                Functionality.AnonsaiParts an = (Functionality.AnonsaiParts)listBox10.Items[location];
                Functionality.AnonsaiPartsById(an.id);
                listBox10.Items.RemoveAt(location);
                textBox8.Text = "";

            }
        }

        private void button30_Click(object sender, RoutedEventArgs e)
        {
            if (!textBox8.Text.Equals(""))
            {
                Functionality.AddAnonsasPart("1", Functionality.rpl(textBox8.Text.ToUpper()));
                Functionality.getAnonsaiPartsByANId("1", listBox10);
                textBox8.Text = "";
            }
        }

        private void button32_Click(object sender, RoutedEventArgs e)
        {
            int location = listBox10.SelectedIndex;
            if (location >= 0)
            {
                Functionality.AnonsaiParts an = (Functionality.AnonsaiParts)listBox10.Items[location];
                Functionality.EditAnonsaiPartsByANid(an.id.ToString(), textBox8.Text.ToUpper());
                Functionality.getAnonsaiPartsByANId("1",listBox10);
                textBox8.Text = "";

            }
        }

        private void Image_ImageFailed_2(object sender, ExceptionRoutedEventArgs e)
        {

        }

        private void textBox7_KeyUp(object sender, KeyEventArgs e)
        {
            if (textBox7.Text.Length == 41)
            {
                string[] words = textBox7.Text.Split(' ');
                textBox7.Text = "";
                for (int i=0;i<words.Length-2;i++){
                    textBox7.Text = textBox7.Text + words[i] + " ";
                }
                
                textBox6.Text = words[words.Length-1];
                textBox6.Focus();
                textBox6.Select(textBox6.Text.Length, 0);
            }
            label21.Content = textBox7.Text.Length.ToString() + "/41";
            if (textBox7.Text.Length > 35) { label21.Foreground = Brushes.Red; }
            else { label21.Foreground = Brushes.Black; }

        }

        private void textBox11_KeyUp(object sender, KeyEventArgs e)
        {
          
        }

        private void textBox15_TextChanged(object sender, TextChangedEventArgs e)
        {

        }

        private void textBox15_KeyUp(object sender, KeyEventArgs e)
        {
           
        }

        private void button34_Click(object sender, RoutedEventArgs e)
        {
            Functionality.getreports(listBox1);
        }

        private void Image_ImageFailed_3(object sender, ExceptionRoutedEventArgs e)
        {
            button1_Click(sender, e);
        }

        private void textBox16_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Enter)
            {
                AddRep();
            }
        }

        private void button35_Click(object sender, RoutedEventArgs e)
        {
            AddRep();
        }

        private void AddRep()
        {
            if (!textBox16.Text.Equals(""))
            {
                listBox3.Items.Add(new Functionality.Reports() { id = 0, name = textBox16.Text, status = "0" });
            }
            textBox1.Text = "";
            Functionality.saveReports(listBox3);
            Functionality.getreports(listBox3);
        }

        private void Image_ImageFailed_4(object sender, ExceptionRoutedEventArgs e)
        {

        }

        private void button36_Click(object sender, RoutedEventArgs e)
        {
            RemoveRep();
        }

        private void listBox3_KeyUp(object sender, KeyEventArgs e)
        {
            RemoveRep();
        }

        private void RemoveRep()
        {
            System.Collections.IList ls = listBox3.SelectedItems;
            for (int i = 0; i < ls.Count; i++)
            {
                int location = listBox3.Items.IndexOf(ls[i]);
                Functionality.Reports rp = (Functionality.Reports)listBox3.Items[location];
                if (rp.id != 0)
                {
                    Functionality.removeReportById(rp.id);
                    listBox3.Items.RemoveAt(location);

                }
                else
                {
                    listBox3.Items.RemoveAt(location);
                }
            }
        }

        private void checkBox3_Checked(object sender, RoutedEventArgs e)
        {
           
        }

        private void Image_ImageFailed_5(object sender, ExceptionRoutedEventArgs e)
        {

        }

        private void button37_Click(object sender, RoutedEventArgs e)
        {
            int location = Functionality.CurrentRPindex;
            if (location >= 0 && !textBox16.Text.Equals(""))
            {
               // Functionality.Reports rp = (Functionality.Reports)listBox3.Items[location];

                listBox3.Items.Insert(location, new Functionality.Reports() { id = Functionality.CurrentRPid, name = textBox16.Text, status = Functionality.CurrentRPstatus });
                listBox3.Items.RemoveAt(location + 1);
                textBox3.Text = "";
            }
            Functionality.saveReports(listBox3);
            Functionality.getreports(listBox3);
        }

        private void checkBox3_Click(object sender, RoutedEventArgs e)
        {
            if (checkBox3.IsChecked == true)
            {
                timer.Start();
            }
            else
            {
                timer.Stop();
            }
        }

        private void listBox3_MouseDoubleClick(object sender, MouseButtonEventArgs e)
        {
           
        }

        private void listBox3_MouseUp(object sender, MouseButtonEventArgs e)
        {
            int location = listBox3.SelectedIndex;
            if (location >= 0)
            {
                Functionality.Reports rp = (Functionality.Reports)listBox3.Items[location];
                textBox16.Text = rp.name;
                Functionality.CurrentRPid = rp.id;
                Functionality.CurrentRPstatus = rp.status;
                Functionality.CurrentRPindex = location;
                Functionality.getAnonsaiByRepId(rp.id.ToString(), listBox4);

                groupBox1.IsEnabled = true;
                listBox5.Items.Clear();
                listBox5.IsEnabled = false;
                textBox4.IsEnabled = false;
                button10.IsEnabled = false;
                button12.IsEnabled = false;


                Functionality.getLydaiByRepId(rp.id.ToString(), listBox6);
                groupBox2.IsEnabled = true;
                listBox7.Items.Clear();
                listBox7.IsEnabled = false;
                textBox6.IsEnabled = false;
                textBox7.IsEnabled = false;
                button14.IsEnabled = false;
                button15.IsEnabled = false;

                groupBox3.IsEnabled = true;
                Functionality.getLogoByRepId(rp.id.ToString(), listBox8);

                groupBox4.IsEnabled = true;
                Functionality.getTitraiByRepId(rp.id.ToString(), listBox9);

            }
        }

        private void textBox6_KeyUp(object sender, KeyEventArgs e)
        {
            label22.Content = textBox6.Text.Length.ToString() + "/41";
            if (textBox6.Text.Length > 35) { label22.Foreground = Brushes.Red; }
            else { label22.Foreground = Brushes.Black; }
            if (e.Key == Key.Enter)
            {
                button14_Click(sender, null);
            }
        }

        private void textBox12_KeyUp(object sender, KeyEventArgs e)
        {

        }

        private void textBox5_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Enter)
            {
                button13_Click(sender,null);
            }
        }

        private void textBox13_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Enter)
            {
                button20_Click(sender, null);
            }
        }

        private void comboBox2_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Enter)
            {
                button17_Click(sender, null);
            }
        }

        private void listBox9_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Delete) { button22_Click(sender, null); textBox12.Text = ""; textBox13.Text = ""; textBox14.Text = "00:00"; textBox15.Text = "00:00"; }
        }

        private void listBox7_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Delete) { button15_Click(sender, null); textBox6.Text = ""; textBox7.Text = ""; }
        }

        private void listBox6_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Delete) { button16_Click(sender, null); textBox5.Text="";}
        }

        private void listBox8_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Delete) { button19_Click(sender, null); textBox9.Text = ""; comboBox2.Text = ""; }
        }

        private static object GetDataFromListBox(ListBox source, Point point)
        {
            UIElement element = source.InputHitTest(point) as UIElement;
            if (element != null)
            {
                object data = DependencyProperty.UnsetValue;
                while (data == DependencyProperty.UnsetValue)
                {
                    data = source.ItemContainerGenerator.ItemFromContainer(element);

                    if (data == DependencyProperty.UnsetValue)
                    {
                        element = VisualTreeHelper.GetParent(element) as UIElement;
                    }

                    if (element == source)
                    {
                        return null;
                    }
                }

                if (data != DependencyProperty.UnsetValue)
                {
                    return data;
                }
            }

            return null;
        }

        private void ListBox_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            ListBox parent = (ListBox)sender;
            dragSource = parent;
            object data = GetDataFromListBox(dragSource, e.GetPosition(parent));

            if (data != null)
            {
                DragDrop.DoDragDrop(parent, data, DragDropEffects.Move);
            }
        }

        private static bool IsMouseOverTarget(Visual target, Point point)
        {
            var bounds = VisualTreeHelper.GetDescendantBounds(target);
            return bounds.Contains(point);
        }

        private void ListBox_Drop(object sender, DragEventArgs e)
        {
            ListBox parent = (ListBox)sender;
            object data = e.Data.GetData(typeof(Functionality.LydaiParts));
            parent.Items.Remove(data);
           // ((IList)dragSource.ItemsSource).Remove(data);
            int index = -1;
            for (int i = 0; i < listBox7.Items.Count; i++)
            {
                var lbi = listBox7.ItemContainerGenerator.ContainerFromIndex(i) as ListBoxItem;
                if (lbi == null) continue;
                if (IsMouseOverTarget(lbi, e.GetPosition((IInputElement)lbi)))
                {
                    index = i;
                    break;
                }
            }
            parent.Items.Insert(index, data);
        }

        private void ListBox_Drop1(object sender, DragEventArgs e)
        {
            ListBox parent = (ListBox)sender;
            object data = null;
            int obix = 1;
            data = e.Data.GetData(typeof(Functionality.Lydai));
            if (data == null)
            {
                obix = 2;
                data = e.Data.GetData(typeof(Functionality.Titras));
            }
            int index = -1;
            for (int i = 0; i < listBox3.Items.Count; i++)
            {
                var lbi = listBox3.ItemContainerGenerator.ContainerFromIndex(i) as ListBoxItem;
                if (lbi == null) continue;
                if (IsMouseOverTarget(lbi, e.GetPosition((IInputElement)lbi)))
                {
                    index = i;
                    break;
                }
            }
            
            if (obix == 1)
            {
                Functionality.insertLydas((Functionality.Lydai)data, (Functionality.Reports)listBox3.Items[index]);
            }
            if (obix == 2)
            {
                Functionality.insertTitras((Functionality.Titras)data, (Functionality.Reports)listBox3.Items[index]);
            }
            //parent.Items.Insert(index, data);
        }

        private void listBox7_PreviewMouseMove(object sender, MouseEventArgs e)
        {

        }
    }
}
